package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoXiaoGan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 安心分期-龙岩市企诺API全量15 孝感
 * @ Author 李源聪
 */
@ApiPlan(productId = "306", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYanQiNuoXiaoGan implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "bj25rtsfpo90snma";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_JSON);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> newMap.put("mobile", apiDataExchangeParameter.getPhoneMd5())));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_JSON);
        OrgApplyLongYanQiNuoXiaoGan data = new OrgApplyLongYanQiNuoXiaoGan();
        //拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //姓名
        data.setName(apiApplyParameter.getUserName());
        //手机号码
        data.setMobile(apiApplyParameter.getPhone());
        //年龄(若没有传0)
        data.setAge(apiFieldConverter.getAgeInt());
        //性别(不传默认0。男传1、女传2)
        if (ValidParam.isEmpty(apiApplyParameter.getSex())) {
            Integer newSex = ChooseEq.create()
                    .when(apiApplyParameter.getSex(), "0", 1)
                    .when(apiApplyParameter.getSex(), "1", 2)
                    .end(1);
            data.setSex(newSex);
        }
        //城市(如天津市)
        data.setCity(apiApplyParameter.getCity());
        //是否有房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", false)
                    .when(apiApplyParameter.getRealEstateInfo(), "2", true)
                    .when(apiApplyParameter.getRealEstateInfo(), "3", true)
                    .when(apiApplyParameter.getRealEstateInfo(), "4", true)
                    .when(apiApplyParameter.getRealEstateInfo(), "5", true)
                    .when(apiApplyParameter.getRealEstateInfo(), "6", true)
                    .end(false);
            data.setIs_house(value);
        }

        //是否有车
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", false)
                    .when(apiApplyParameter.getCarInfo(), "2", true)
                    .when(apiApplyParameter.getCarInfo(), "3", true)
                    .when(apiApplyParameter.getCarInfo(), "4", true)
                    .end(false);
            data.setIs_car(value);
        }
        // 信用卡(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "2", true)
                    .when(apiApplyParameter.getCreditLimit(), "3", true)
                    .when(apiApplyParameter.getCreditLimit(), "4", true)
                    .when(apiApplyParameter.getCreditLimit(), "5", true)
                    .when(apiApplyParameter.getCreditLimit(), "1", false)
                    .when(apiApplyParameter.getCreditLimit(), "6", true)
                    .end(false);
            data.setIs_credit(value);
        }
        //是否有保单
        // 保单(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", false)
                    .when(apiApplyParameter.getInsurancePolicy(), "2", true)
                    .when(apiApplyParameter.getInsurancePolicy(), "3", true)
                    .when(apiApplyParameter.getInsurancePolicy(), "4", true)
                    .end(false);
            data.setIs_insurance(value);
        }
        // 社保(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", false)
                    .when(apiApplyParameter.getSocialSecurity(), "2", true)
                    .when(apiApplyParameter.getSocialSecurity(), "3", true)
                    .when(apiApplyParameter.getSocialSecurity(), "4", true)
                    .end(false);
            data.setIs_social(value);
        }
        // 公积金(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", false)
                    .when(apiApplyParameter.getProvidentFund(), "2", true)
                    .when(apiApplyParameter.getProvidentFund(), "3", true)
                    .when(apiApplyParameter.getProvidentFund(), "4", true)
                    .end(false);
            data.setIs_fund(value);
        }
        //是否有打卡工资
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            boolean value = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", true)
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", true)
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", true)
                    .end(false);
            data.setIs_work(value);
        }
        //申请额度（单位:万元）
        data.setMoney_demand(apiFieldConverter.getLoanAmountInt());
        //备注
        data.setRemark(apiFieldConverter.getExtendInfo());
        String json = JsonUtils.toJson(data);
        AES ecb = AES.createEcb(KEY.getBytes());
        String encryptData = ecb.encryptToBase64Str(json);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> newMap.put("data", encryptData)));
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
